package cc.admin.modules.wea.service.impl;

import cc.admin.modules.wea.entity.WeaPlatform;
import cc.admin.modules.wea.entity.WeaPredictRoute;
import cc.admin.modules.wea.entity.WeaPredictRouteDetail;
import cc.admin.modules.wea.entity.WeatherConstant;
import cc.admin.modules.wea.mapper.WeaPredictRouteMapper;
import cc.admin.modules.wea.service.IWeaPlatformService;
import cc.admin.modules.wea.service.IWeaPredictRouteDetailService;
import cc.admin.modules.wea.service.IWeaPredictRouteService;
import cc.admin.modules.wea.utils.WeatherUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * @Description: 航线预测表
 * @Author: cc-admin
 * @Date:   2024-08-30
 * @Version: V1.0.0
 */
@Service
public class WeaPredictRouteServiceImpl extends ServiceImpl<WeaPredictRouteMapper, WeaPredictRoute> implements IWeaPredictRouteService {


  @Autowired
  private IWeaPredictRouteService weaPredictRouteService;
  @Autowired
  private IWeaPlatformService weaPlatformService;
  @Autowired
  private IWeaPredictRouteDetailService weaPredictRouteDetailService;


  @Override
  public List<Map<String, Object>> sevenDayWeather(String id) {
    List<Map<String,Object>> mapList =new ArrayList<>();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    WeaPredictRoute weaPredictRoute=weaPredictRouteService.getById(id);
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq("parent_id", weaPredictRoute.getId());
    List<WeaPredictRouteDetail> weaPredictRouteDetails = weaPredictRouteDetailService.list();
    Map<String,String> map = new HashMap<>();
//    获取所有坐标
    weaPredictRouteDetails.forEach(item->{
        map.put(sdf.format(item.getRecordTime()), item.getLon()+","+item.getLat());
    });
//    获取当前日期的时间列表
    List<String> sevenDaysDate = getDatePlusSevenDays();
//    当天坐标天气
    Date date = new Date();
    WeaPlatform weaPlatform=weaPlatformService.getById(weaPredictRoute.getPlatformId());
    String dayWeather=WeatherUtil.getWeather(WeatherConstant.SEVEN_DAY_FORECAST,weaPlatform.getLon()+","+weaPlatform.getLat());
    JSONArray list=JSONUtil.parseArray(dayWeather);
    for (int i = 0; i < list.size(); i++) {
      String dateTime = list.getJSONObject(i).getStr("dateTime");
      if (sdf.format(date).equals(dateTime)){
        mapList.add(list.getJSONObject(i));
      }
    }
    sevenDaysDate.forEach(item->{
      if(map.get(item)!=null){
        String sevenDayWeather=WeatherUtil.getWeather(WeatherConstant.SEVEN_DAY_FORECAST,map.get(item));
        JSONArray map1=JSONUtil.parseArray(sevenDayWeather);
        for (int i = 0; i < map1.size(); i++) {
          String dateTime = map1.getJSONObject(i).getStr("dateTime");
          if (item.equals(dateTime)){
            mapList.add(map1.getJSONObject(i));
          }
        }
      }
    });
    return mapList;
  }

  public static void main(String[] args) {
    List<String> sevenDaysDate = getDatePlusSevenDays();
    System.out.println("当前日期后 7 天的日期：" + sevenDaysDate);
  }
  public static List<String> getDatePlusSevenDays() {
    List<String> list = new ArrayList<>();
    LocalDate currentDate = LocalDate.now();
    for (int i = 1; i <= 7; i++) {
      LocalDate nextDate = currentDate.plusDays(i);
      String formattedDate = nextDate.getYear() + "-" + String.format("%02d", nextDate.getMonthValue()) + "-" + String.format("%02d", nextDate.getDayOfMonth());
      list.add(formattedDate);
    }
    return list;
  }
}
